package com.seis_645.appTesting;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class SaveDealerActivity extends Activity {
	private Spinner spinner_array[] = new Spinner[3];
	private EditText name, street, city, state, zipcode;
	int cleanliness, friendliness, supply;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
				R.layout.add_dealer, null);
		this.setContentView(viewToLoad);

		spinner_array[0] = (Spinner) findViewById(R.id.cleanlinessSpinner);
		spinner_array[1] = (Spinner) findViewById(R.id.friendlinessSpinner);
		spinner_array[2] = (Spinner) findViewById(R.id.supplySpinner);
		
		Button cancel = (Button) findViewById(R.id.btn_cancel_dealer);
		Button save = (Button) findViewById(R.id.btn_save_dealer);
		OnTouchListener cancelListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					finish();
					// return true;
				}
				return false;
			}
		};
		
		OnTouchListener saveListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_UP) {
					addToDatabase();
					finish();
				}
				return false;
			}
		};
		
		cancel.setOnTouchListener(cancelListener);
		save.setOnTouchListener(saveListener);
	}

	private void addToDatabase() {
		int value;
		for (int i = 0; i < spinner_array.length; i++) {
			String compare = spinner_array[i].getSelectedItem().toString();
			if (compare.equalsIgnoreCase("excellent")
					|| compare.equalsIgnoreCase("five")) {
				value = 5;
			} else if (compare.equalsIgnoreCase("very good")
					|| compare.equalsIgnoreCase("four")) {
				value = 4;
			} else if (compare.equalsIgnoreCase("good")
					|| compare.equalsIgnoreCase("three")) {
				value = 3;
			} else if (compare.equalsIgnoreCase("fair")
					|| compare.equalsIgnoreCase("two")) {
				value = 2;
			} else {
				value = 1;
			}
			switch (i) {
			case 0:
				cleanliness = value;
				break;
			case 1:
				friendliness = value;
				break;
			case 2:
				supply = value;
				break;
			}
		}
		
		
		name = (EditText) findViewById(R.id.name);
		street = (EditText) findViewById(R.id.street);
		city = (EditText) findViewById(R.id.city);
		state = (EditText) findViewById(R.id.state);
		zipcode = (EditText) findViewById(R.id.zipcode);
		
		
		System.out.println(name.getText().toString() + " "
				+ street.getText().toString() + " " + city.getText().toString()
				+ " " + state.getText().toString()+ " "+zipcode.getText().toString());
		System.out
				.println(Integer.toString(cleanliness) + " "
						+ Integer.toString(friendliness) + " "
						+ Integer.toString(supply));
		
		
		DealerDataSource datasource = new DealerDataSource(this);
		datasource.open();
		datasource.create_dealer(name.getText().toString(),street.getText().toString(),
			city.getText().toString(), state.getText().toString(), zipcode.getText().toString(), 
			cleanliness, friendliness, supply);
		
		datasource.close();
	}
}
